cmake_minimum_required(VERSION 3.21 FATAL_ERROR)

set(CMAKE_CXX_EXTENSIONS FALSE)

project(pw_pbl CXX)

add_library(pw_pbl STATIC
src/Coroutine/QueueExecutor.cpp
src/Coroutine/VectorExecutor.cpp
src/DigitEditStr.cpp
src/Memory.cpp
)

target_include_directories(pw_pbl PUBLIC include)

target_compile_features(pw_pbl PUBLIC cxx_std_26)

add_subdirectory(TLSF)

option(PW_PBL_FORMAT "enable format lib, if enabled and 'fmt' package not added before then it will be add by CPM" ON)
if(PW_PBL_FORMAT)
	include(FetchContent)

	# populate CPM if not populate
	FetchContent_GetProperties(CPM POPULATED cpm_POPULATED)
	if(NOT cpm_POPULATED)
		FetchContent_Declare(
			CPM
			URL "https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.42.0/CPM.cmake"
			DOWNLOAD_NO_EXTRACT TRUE
		)
		FetchContent_MakeAvailable(CPM)
		include(${cpm_SOURCE_DIR}/CPM.cmake)
		CPMUsePackageLock(package-lock.cmake)
	endif()

	CPMAddPackage(
		NAME fmt
		OPTIONS "FMT_OS OFF" "FMT_SYSTEM_HEADERS ON"
		GIT_REPOSITORY https://github.com/fmtlib/fmt.git
		GIT_TAG 11.2.0
		GIT_SHALLOW TRUE
		GIT_PROGRESS TRUE
		EXCLUDE_FROM_ALL
	)
	if(fmt_ADDED)
		target_compile_definitions(fmt PUBLIC $<$<COMPILE_LANGUAGE:CXX>:FMT_BUILTIN_TYPES=0 FMT_USE_LOCALE=0 FMT_USE_EXCEPTIONS=0 FMT_OPTIMIZE_SIZE=1>)
		target_compile_options(fmt PUBLIC "$<$<COMPILE_LANGUAGE:CXX>:-DFMT_THROW(x)=abort()>")
		target_compile_features(fmt PUBLIC cxx_std_23)
	endif(fmt_ADDED)

	target_compile_definitions(fmt PUBLIC PW_PBL_FORMAT=1) # 本应添加到pw_pbl库，为满足clangd推导改为添加到fmt库
	target_link_libraries(pw_pbl PUBLIC fmt)
endif(PW_PBL_FORMAT)
